package com.example.administrator.player;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    public static final int PLAY = 1;
    public static final int PAUSE = 2;
    public static final int REPLAY = 3;
    public static final int LASTORNEXT = 4;
    public static final int JUMP = 5;
    public static Integer flag = 0;

    private LinearLayout linear;
    public static SeekBar seekbar;

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        linear = (LinearLayout) findViewById(R.id.linearlayout);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        seekbar.setOnSeekBarChangeListener(this);
        List<String> sList = getSongsList();
        TextView tv;
        String text;
        if (sList != null && sList.size() > 0) {
            for (int i = 0; i < sList.size(); i++) {
                tv = new TextView(this);
                text = sList.get(i);
                tv.setText(text);
                tv.setTextColor(getResources().getColor(R.color.songColor));
                tv.setHeight(100);
                tv.setGravity(Gravity.CENTER_VERTICAL);
                tv.setTextSize(22);
                Log.i("TestCase", "test" + text);
                linear.addView(tv);
            }
        }
    }

    public static List<String> getSongsList() {
        List<String> sList = new ArrayList<String>();
        File file = new File("/sdcard/Music");
        File[] f = file.listFiles();
        if (f != null && f.length > 0) {
            for (int i = 0; i < f.length; i++) {
                if (f[i].getName().endsWith(".mp3")) {
                    sList.add(f[i].getName());
                }
            }
            return sList;
        }
        return null;
    }


    public void last(View v) {
        Button btn = (Button) findViewById(R.id.btn_play_pause);
        if (flag != 0) flag--;
        btn.setText("暂停");
        Intent intent = new Intent("music");
        intent.putExtra("action", LASTORNEXT);
        startService(intent);
    }

    public void playOrPause(View v) {
        Button btn = (Button) findViewById(R.id.btn_play_pause);
        String text = btn.getText().toString();
        Intent intent = new Intent("music");
        if (text.equals("播放")) {
            btn.setText("暂停");
            intent.putExtra("action", PLAY);
            startService(intent);
        } else if (text.equals("暂停")) {
            btn.setText("播放");
            intent.putExtra("action", PAUSE);
            startService(intent);
        }
    }

    public void replay(View v) {
        Intent intent = new Intent("music");
        intent.putExtra("action", REPLAY);
        startService(intent);
    }

    public void next(View v) {
        Button btn = (Button) findViewById(R.id.btn_play_pause);
        if (flag != getSongsList().size()) flag++;
        btn.setText("暂停");
        Intent intent = new Intent("music");
        intent.putExtra("action", LASTORNEXT);
        startService(intent);
        getSeekBar(intent);
    }

    public void getSeekBar(Intent intent) {
            bindService(intent, new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder proxy) {
                    MusicService.MyBinder mybinder = (MusicService.MyBinder)proxy;
                    int max = mybinder.getDuration();
                    mybinder.getProgress();
                    Log.i("TestCase",Thread.currentThread().getName()+"==>thread name");
                    seekbar.setMax(max);

                }

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }
            }, BIND_AUTO_CREATE);

    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int pro = seekbar.getProgress();
        Intent intent = new Intent("music");
        intent.putExtra("action",JUMP);
        intent.putExtra("progress",pro);
        Log.i("TestCase",pro +"==>progress");
        startService(intent);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
